package com.usaas.search.service.impl;
import java.util.Date;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.usaas.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.util.StrUtil;
import com.usaas.search.mapper.PlatUserMapper;
import com.usaas.search.pojo.PlatUserPojo;
import com.usaas.search.dto.PlatUserDto;
import com.usaas.search.service.IPlatUserService;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.collection.ListUtil;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * 系统用户Service业务层处理
 * 
 * @author Helios
 * @date 2024-08-14
 */
@Slf4j
@Service
public class PlatUserServiceImpl extends ServiceImpl<PlatUserMapper,PlatUserPojo> implements IPlatUserService {

    @Resource
    private PlatUserMapper platUserMapper;

    /**
     * 查询系统用户
     * 
     * @param id 系统用户主键
     * @return 系统用户
     */
    @Override
    public PlatUserDto queryById(Long id) {
        PlatUserPojo pojo = this.getById(id);
        if (pojo != null) {
            PlatUserDto dto= BeanUtil.copyProperties(pojo, PlatUserDto.class);
            List<PlatUserDto> dtos = ListUtil.of(dto);
            convertList(dtos);
            return dtos.get(0);
        }
        return null;
    }

    /**
     * 查询系统用户列表
     * 
     * @param platUserDto 系统用户
     * @return 系统用户
     */
    @Override
    public List<PlatUserDto> queryList(PlatUserDto platUserDto) {
        LambdaQueryWrapper<PlatUserPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper =this.buildWrapper(queryWrapper, platUserDto);
        if (queryWrapper == null) {
            return null;
        }
        List<PlatUserPojo> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        List<PlatUserDto> dtoList = BeanUtil.copyToList(list, PlatUserDto.class);
        convertList(dtoList);
        return dtoList;
    }

    private void convertList(List<PlatUserDto> dtoList) {
        if(CollectionUtil.isEmpty(dtoList)){
            return;
        }
    }

    /**
     * 新增系统用户
     * 
     * @param platUserDto 系统用户
     * @return 结果
     */
    @Override
    @Transactional
    public boolean add(PlatUserDto platUserDto) {

        PlatUserPojo platUserPojo = BeanUtil.copyProperties(platUserDto, PlatUserPojo.class);
    return this.save(platUserPojo);
    }

    /**
     * 修改系统用户
     * 
     * @param platUserDto 系统用户
     * @return 结果
     */
    @Override
    @Transactional
    public boolean update(PlatUserDto platUserDto) {
            PlatUserPojo platUserPojo = BeanUtil.copyProperties(platUserDto, PlatUserPojo.class);
        return this.updateById(platUserPojo);
    }

    /**
     * 批量删除系统用户
     *
     * @param ids 需要删除的系统用户主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeBatch(List<String> ids) {
        List<Long> idsLong = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return this.removeBatchByIds(idsLong);
    }



    /**
     * 分页查询${subTable.functionName}信息
     * @param platUserDto
     * @return
     */
    @Override
    public AjaxResult queryPage(PlatUserDto platUserDto) {
        Page<PlatUserPojo> page = new Page<>(platUserDto.getPageNum(), platUserDto.getPageSize());
        LambdaQueryWrapper<PlatUserPojo> wrapper = new LambdaQueryWrapper<PlatUserPojo>();
        //此处可做前置判断
        if(wrapper==null){
            return AjaxResult.success(page);
        }
        wrapper=buildWrapper(wrapper,platUserDto);
        if(wrapper==null){
            return AjaxResult.success(page);
        }
        Page<PlatUserPojo> pageResult = this.page(page, wrapper);
        Page<PlatUserDto> finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatUserPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatUserDto> dtos = BeanUtil.copyToList(records, PlatUserDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return AjaxResult.success(finalResult);
    }

    private LambdaQueryWrapper<PlatUserPojo> buildWrapper(LambdaQueryWrapper<PlatUserPojo> wrapper, PlatUserDto param) {
        log.info("buildPageWrapper param :{}", param);
        if (wrapper == null) {
            return null;
        }
            Long id=param.getId();
                if (id !=null){
                    wrapper.eq(PlatUserPojo::getId, id);
            }
            String userName=param.getUserName();
                if (StrUtil.isNotBlank(userName)) {
                    wrapper.eq(PlatUserPojo::getUserName, userName);
                }
            String userPassword=param.getUserPassword();
                if (StrUtil.isNotBlank(userPassword)) {
                    wrapper.eq(PlatUserPojo::getUserPassword, userPassword);
                }
            String unionid=param.getUnionid();
                if (StrUtil.isNotBlank(unionid)) {
                    wrapper.eq(PlatUserPojo::getUnionid, unionid);
                }
            String openid=param.getOpenid();
                if (StrUtil.isNotBlank(openid)) {
                    wrapper.eq(PlatUserPojo::getOpenid, openid);
                }
            String headImg=param.getHeadImg();
                if (StrUtil.isNotBlank(headImg)) {
                    wrapper.eq(PlatUserPojo::getHeadImg, headImg);
                }
            String nickname=param.getNickname();
                if (StrUtil.isNotBlank(nickname)) {
                    wrapper.eq(PlatUserPojo::getNickname, nickname);
                }
            String phonenum=param.getPhonenum();
                if (StrUtil.isNotBlank(phonenum)) {
                    wrapper.eq(PlatUserPojo::getPhonenum, phonenum);
                }
            String realName=param.getRealName();
                if (StrUtil.isNotBlank(realName)) {
                    wrapper.eq(PlatUserPojo::getRealName, realName);
                }
            String idcard=param.getIdcard();
                if (StrUtil.isNotBlank(idcard)) {
                    wrapper.eq(PlatUserPojo::getIdcard, idcard);
                }
            Long accountStatus=param.getAccountStatus();
                if (accountStatus !=null){
                    wrapper.eq(PlatUserPojo::getAccountStatus, accountStatus);
            }
            Long publishStatus=param.getPublishStatus();
                if (publishStatus !=null){
                    wrapper.eq(PlatUserPojo::getPublishStatus, publishStatus);
            }
            Long updateHeadCounts=param.getUpdateHeadCounts();
                if (updateHeadCounts !=null){
                    wrapper.eq(PlatUserPojo::getUpdateHeadCounts, updateHeadCounts);
            }
            Long updateNicknameCounts=param.getUpdateNicknameCounts();
                if (updateNicknameCounts !=null){
                    wrapper.eq(PlatUserPojo::getUpdateNicknameCounts, updateNicknameCounts);
            }
            Long userPoints=param.getUserPoints();
                if (userPoints !=null){
                    wrapper.eq(PlatUserPojo::getUserPoints, userPoints);
            }
            String createBy=param.getCreateBy();
                if (StrUtil.isNotBlank(createBy)) {
                    wrapper.eq(PlatUserPojo::getCreateBy, createBy);
                }
            Date createTime=param.getCreateTime();
                if (createTime !=null){
                    wrapper.eq(PlatUserPojo::getCreateTime, createTime);
            }
            String updateBy=param.getUpdateBy();
                if (StrUtil.isNotBlank(updateBy)) {
                    wrapper.eq(PlatUserPojo::getUpdateBy, updateBy);
                }
            Date updateTime=param.getUpdateTime();
                if (updateTime !=null){
                    wrapper.eq(PlatUserPojo::getUpdateTime, updateTime);
            }
            String remark=param.getRemark();
                if (StrUtil.isNotBlank(remark)) {
                    wrapper.eq(PlatUserPojo::getRemark, remark);
                }
            Long delFlag=param.getDelFlag();
                if (delFlag !=null){
                    wrapper.eq(PlatUserPojo::getDelFlag, delFlag);
            }
        log.info("buildPageWrapper result :{}", wrapper);
        return wrapper;
    }
}
